/**
 *  Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.dao;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public abstract class GenericDAO<T> implements DAO<T> {

    private Class<T> persistentClass;

    @PersistenceContext
    EntityManager em;

    @Inject
    public GenericDAO(EntityManager em, Class<T> persistentClass) {
        this.persistentClass = persistentClass;
        this.em = em;
    }

    public GenericDAO(Class<T> persistentClass) {
        this.persistentClass = persistentClass;
    }

    public void persist(T t) {
        em.persist(t);
    }

    public void delete(T t) {
        em.remove(t);
    }

    public void update(T t) {
    	em.merge(t);
    }

    public T merge(T t) {
        em.merge(t);
        return t;
    }
    
    public T findById(Long id) {
        return em.find(persistentClass, id);
    }
    
}
